﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class SingleAccordionStyle3EffectForm : ItemFormTemplate
    {
        public SingleAccordionStyle3EffectForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件效果\\手风琴\\SingleAccordionStyle3EffectForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;


            List<SingleAccordionNode> nodeList = new List<SingleAccordionNode>();
            {
                HML.SingleAccordionContentNode singleAccordionContentNode1 = new HML.SingleAccordionContentNode() { Text = "Dashboard", TBMargin = new TBMargin(0, 10), TextLRMargin = new LRMargin(0, 10), MouseAnimationEnabled = SingleAccordionMouseAnimationEnableds.True, LRAnimationDistance = 10 };
                singleAccordionContentNode1.StyleAppearance.Normal.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionContentNode1.StyleAppearance.Enter.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionContentNode1.StyleAppearance.Selected.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionContentNode1.StyleAppearance.Normal.TextColor = Color.FromArgb(255, 255, 255);
                singleAccordionContentNode1.StyleAppearance.Enter.TextColor = Color.FromArgb(255, 255, 255);
                singleAccordionContentNode1.StyleAppearance.Selected.TextColor = Color.FromArgb(255, 255, 255);
                nodeList.Add(singleAccordionContentNode1);

                SingleAccordionCategoryNode singleAccordionCategoryNode0 = new HML.SingleAccordionCategoryNode() { Text = "Articles", TBMargin = new TBMargin(0, 10), TextLRMargin = new LRMargin(0, 10), MouseAnimationEnabled = SingleAccordionMouseAnimationEnableds.True, LRAnimationDistance = 10 };
                singleAccordionCategoryNode0.StyleAppearance.Normal.BackColor = Color.FromArgb(255, 255, 255);
                singleAccordionCategoryNode0.StyleAppearance.Enter.BackColor = Color.FromArgb(255, 255, 255);
                singleAccordionCategoryNode0.StyleAppearance.Normal.TextColor = Color.FromArgb(51, 51, 51);
                singleAccordionCategoryNode0.StyleAppearance.Enter.TextColor = Color.FromArgb(51, 51, 51);
                nodeList.Add(singleAccordionCategoryNode0);
                singleAccordionCategoryNode0.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Write a new Article", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode0.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Manage Articles", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode0.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Manage Comments", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode0.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Manage Categories", TextLRMargin = new LRMargin(0, 10) });

                SingleAccordionCategoryNode singleAccordionCategoryNode1 = new HML.SingleAccordionCategoryNode() { Text = "Pages", TBMargin = new TBMargin(0, 10), TextLRMargin = new LRMargin(0, 10), MouseAnimationEnabled = SingleAccordionMouseAnimationEnableds.True, LRAnimationDistance = 10 };
                singleAccordionCategoryNode1.StyleAppearance.Normal.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode1.StyleAppearance.Enter.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode1.StyleAppearance.Normal.TextColor = Color.FromArgb(255, 255, 255);
                singleAccordionCategoryNode1.StyleAppearance.Enter.TextColor = Color.FromArgb(255, 255, 255);
                nodeList.Add(singleAccordionCategoryNode1);
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Create a new Page", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode1.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Manage Pages", TextLRMargin = new LRMargin(0, 10) });

                SingleAccordionCategoryNode singleAccordionCategoryNode2 = new HML.SingleAccordionCategoryNode() { Text = "Image Gallery", TBMargin = new TBMargin(0, 10), TextLRMargin = new LRMargin(0, 10), MouseAnimationEnabled = SingleAccordionMouseAnimationEnableds.True, LRAnimationDistance = 10 };
                singleAccordionCategoryNode2.StyleAppearance.Normal.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode2.StyleAppearance.Enter.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode2.StyleAppearance.Normal.TextColor = Color.FromArgb(255, 255, 255);
                singleAccordionCategoryNode2.StyleAppearance.Enter.TextColor = Color.FromArgb(255, 255, 255);
                nodeList.Add(singleAccordionCategoryNode2);
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Upload Images", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Manage Galleries", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Manage Albums", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode2.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Gallery Settings", TextLRMargin = new LRMargin(0, 10) });

                SingleAccordionCategoryNode singleAccordionCategoryNode3 = new HML.SingleAccordionCategoryNode() { Text = "Events Calendar", TBMargin = new TBMargin(0, 10), TextLRMargin = new LRMargin(0, 10), MouseAnimationEnabled = SingleAccordionMouseAnimationEnableds.True, LRAnimationDistance = 10 };
                singleAccordionCategoryNode3.StyleAppearance.Normal.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode3.StyleAppearance.Enter.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode3.StyleAppearance.Normal.TextColor = Color.FromArgb(255, 255, 255);
                singleAccordionCategoryNode3.StyleAppearance.Enter.TextColor = Color.FromArgb(255, 255, 255);
                nodeList.Add(singleAccordionCategoryNode3);
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Calendar Overview", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Add a new Event", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode3.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Calendar Settings", TextLRMargin = new LRMargin(0, 10) });

                SingleAccordionCategoryNode singleAccordionCategoryNode4 = new HML.SingleAccordionCategoryNode() { Text = "Settings", TBMargin = new TBMargin(0, 10), TextLRMargin = new LRMargin(0, 10), MouseAnimationEnabled = SingleAccordionMouseAnimationEnableds.True, LRAnimationDistance = 10 };
                singleAccordionCategoryNode4.StyleAppearance.Normal.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode4.StyleAppearance.Enter.BackColor = Color.FromArgb(121, 185, 0);
                singleAccordionCategoryNode4.StyleAppearance.Normal.TextColor = Color.FromArgb(255, 255, 255);
                singleAccordionCategoryNode4.StyleAppearance.Enter.TextColor = Color.FromArgb(255, 255, 255);
                nodeList.Add(singleAccordionCategoryNode4);
                singleAccordionCategoryNode4.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "General", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode4.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Design", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode4.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Your Profile", TextLRMargin = new LRMargin(0, 10) });
                singleAccordionCategoryNode4.Childrens.Add(new HML.SingleAccordionContentNode() { Text = "Users and Permissions", TextLRMargin = new LRMargin(0, 10) });

            }
            this.demo_control_panel.Size = new Size((int)Math.Ceiling(225 * this.demo_control.ScaleDpi), (int)Math.Ceiling(580 * this.demo_control.ScaleDpi));
            this.demo_control.Alignment = SingleAccordionAlignments.Right;
            this.demo_control.BackColor = Color.Transparent;
            this.demo_control.BorderVisible = false;
            this.demo_control.Font = new Font("Bahnschrift SemiBold", 9.5F);
            this.demo_control.ContentNodeStyle.MouseAnimationAllTimer = 150;
            this.demo_control.ContentNodeStyle.LRAnimationDistance = 0;
            this.demo_control.StyleAppearance.ContentNode.Enter.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ContentNode.Normal.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ContentNode.Selected.BackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ContentNode.Normal.TextColor = Color.White;
            this.demo_control.StyleAppearance.ContentNode.Normal.TextColor = Color.Transparent;
            this.demo_control.StyleAppearance.ContentNode.Selected.TextColor = Color.White;
            this.demo_control.StyleAppearance.ScrollBar.Normal.TrackBackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ScrollBar.Enter.TrackBackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ScrollBar.Normal.ThumbBackColor = Color.Transparent;
            this.demo_control.StyleAppearance.ScrollBar.Enter.ThumbBackColor = Color.Transparent;
            this.demo_control.Nodes.Clear();
            this.demo_control.Nodes.AddRange(nodeList.ToArray());
            this.demo_control.NodeDrawBefore += this.Demo_control_NodeDrawBefore;
            this.demo_control.NodeDrawAfter += this.Demo_control_NodeDrawAfter;
            this.demo_control.NodeClick += this.Demo_control_NodeClick;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control_panel.Height = Math.Min(1000, (control.Height - 80));
            this.demo_control_panel.Location = new Point((control.Width - this.demo_control_panel.Width) / 2, (control.Height - this.demo_control_panel.Height) / 2);
        }

        private void Demo_control_NodeDrawBefore(object sender, SingleAccordionNodeDrawBeforeEventArgs e)
        {
            SingleAccordion sa = (SingleAccordion)sender;

            //设置等级深度为0的节点为圆角背景
            if (e.Node.Level == 0)
            {
                Graphics g = e.Graphics;
                SmoothingMode sm = g.SmoothingMode;

                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.FillPath(e.BackBrush, HML.ControlHelper.AdjustCircularPath(e.Bounds_C, 3, 0, 0, 3));
                g.SmoothingMode = sm;

                g.DrawString(e.Node.Text, sa.Font, e.TextBrush, e.TextBounds_C, StringFormat.GenericTypographic);

                //取消控件默认绘制
                e.Cancel = true;

                g.SmoothingMode = sm;
            }

            //设置类别节点下内容节点文本颜色
            if (e.Node is SingleAccordionContentNode && e.Node.Level > 0)
            {
                if (sa.SelectedNode == e.Node)
                {
                    e.TextBrush.Color = Color.FromArgb(255, 255, 255);
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEnter)
                {
                    e.TextBrush.Color = Color.FromArgb(255, 255, 255);
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseEntering)
                {
                    int rgb = Math.Max(128, Math.Min(byte.MaxValue, 175 + (int)Math.Ceiling((255 - 175) * AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 0.5))));
                    e.TextBrush.Color = Color.FromArgb(rgb, rgb, rgb);
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeaveing)
                {
                    int rgb = Math.Max(128, Math.Min(byte.MaxValue, 175 + (int)Math.Ceiling((255 - 175) * (1 - AnimationHelper.EaseOut(e.Node.MouseAnimationUsedTime, e.Node.GetMouseAnimationAllTime(), 0.5)))));
                    e.TextBrush.Color = Color.FromArgb(rgb, rgb, rgb);
                }
                else if (e.Node.MouseAnimationStatus == SingleAccordionNodeMouseAnimationStatuss.MouseLeave)
                {
                    e.TextBrush.Color = Color.FromArgb(170, 170, 170);
                }
            }

        }

        private void Demo_control_NodeDrawAfter(object sender, SingleAccordionNodeDrawAfterEventArgs e)
        {
            SingleAccordion sa = (SingleAccordion)sender;

            //选中内容节点绘制三角形
            if (e.Node is SingleAccordionContentNode && sa.SelectedNode == e.Node)
            {
                Graphics g = e.Graphics;
                SmoothingMode sm = g.SmoothingMode;

                g.SmoothingMode = SmoothingMode.AntiAlias;
                int center = e.Bounds_C.Y + e.Bounds_C.Height / 2;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                Color back_color_tmp = e.BackBrush.Color;
                e.BackBrush.Color = Color.FromArgb(255, 255, 255);
                g.FillPolygon(e.BackBrush, new PointF[] { new PointF(e.Bounds_C.Right, center - 5), new PointF(e.Bounds_C.Right, center + 5), new PointF(e.Bounds_C.Right - 6, center), });
                e.BackBrush.Color = back_color_tmp;

                g.SmoothingMode = sm;
            }
        }

        private void Demo_control_NodeClick(object sender, SingleAccordionNodeEventArgs e)
        {
            //当单击等级深度为0内容节点时折叠已展开的类别节点
            if (e.Node is SingleAccordionContentNode && e.Node.Level == 0)
            {
                SingleAccordion sa = (SingleAccordion)sender;
                SingleAccordionCategoryNode[] category_node_arr = sa.FindExpand();
                if (category_node_arr.Length > 0)
                {
                    sa.ExpandCollapse(category_node_arr[0], false, true);
                }
            }
        }

        #endregion

    }
}
